import { Component, OnInit, Input, Output, EventEmitter, ViewChild } from '@angular/core';

import { CudTableService } from '../../../public/serve/cud-table/cud-table.service';
import { RemoveTableService } from '../../../public/serve/remove-table/remove-table.service';
import { Pagination } from '../../../public/serve/pcrudInfo';
import { Service } from '../../../server/server';
import { addDatatype } from '../effect-add/effect-add.component';
import { HintsService } from '../../../public/serve/hints/hints.service';
declare var $: any;

@Component({
  selector: 'app-effect-permission',
  templateUrl: './effect-permission.component.html',
})
export class EffectPermissionComponent implements OnInit {

  public selected: any;

  constructor(
    private server: Service, 
    private tableListService: CudTableService, 
    private removeTableService: RemoveTableService,
    private hintsService:HintsService
    ) { }

  ngOnInit() {
    this.effectIdeventEmit = this.server.effectIdeventEmit.subscribe((value: any) => {
      this.functionId = value.functionId;
      this.functionName = value.functionName;
      this.getTabList(1, 200, "0");
      //console.log(this.tabvaled)  
      this.getTabListed(1, 200, this.functionId);
    });
    //选中
    //this.selected =["23ae7a3a30ba46fb91f69566caeb839d", "911dd533dd7f42e7b0cd6e494ccf8057", "891eb540ad484a2892602b00e639dc5c"];

  }
  //取消订阅
  public effectIdeventEmit: any;
  ngOnDestroy() {
    if (this.effectIdeventEmit ) {
      this.effectIdeventEmit.unsubscribe();
    }
  }

  public tabVal = [];  // 全部

  public functionId: string;
  public functionName: string;
  @Output()
  allPermission: EventEmitter<Array<any>> = new EventEmitter();

  // 表格(全部)
  getTabList(pageNumber, pageSize, id) {
    let url = this.server.baseUrl+'admin/operations/getPermissionList';
    let data = new Pagination(pageNumber, pageSize, id);

    this.tableListService.postRequest(url, data).then((data: any) => {
      this.tabVal = data.result;
      this.allPermission.emit(this.tabVal);
    }).catch();
  }


  @Input() getTabVal: addDatatype;

  @Input() superiorId: any;

  @Output()
  permissionOutput: EventEmitter<any> = new EventEmitter();

  public tabValed: Array<any>;
  //选中数据
  getTabListed(pageNumber, pageSize, id) {
    let url = this.server.baseUrl + 'user/functions/getRelationedList';
    let data = new Pagination(pageNumber, pageSize, id);

    this.tableListService.postRequest(url, data).then((data: any) => {
      this.tabValed = this.tabValed ? this.tabValed : [];
      this.opNamelist = this.opNamelist ? this.opNamelist : []
      // this.tabValed  = data.result;
      //console.log(data);
      if (data.result != null) {
        this.tabValed = [];
        this.opNamelist = [];
        for (let each of data.result) {
          this.tabValed = [...this.tabValed, each.operationId]
          this.opNamelist = [...this.opNamelist, each.operationName]
        }
      } else {
        this.tabValed = [];
        this.opNamelist = [];
      }


    }).catch();
  }

  //选中
  //public selected :any;
  public tabvaled: Array<string> = [];   // 选中id
  public opNamelist: Array<any> = [];  //选中name
  getSysVal(operationId, operationName) {
    let index = this.tabValed.findIndex(id => operationId == id)
    let indexname = this.opNamelist.findIndex(name => operationName == name)
    let exist = index != -1
    let existName = indexname != -1

    if (exist) {
      this.tabValed.splice(index, 1)
    } else {
      this.tabValed.push(operationId)
    }
    if (existName) {
      this.opNamelist.splice(indexname, 1)
    } else {
      this.opNamelist.push(operationName)
    }
    this.isSelected(operationId)
  }

  isSelected(operationId) {
    //console.log(operationId)
    this.tabValed = this.tabValed ? this.tabValed : [];
    return this.tabValed.findIndex(id => operationId == id) != -1;
  }

  selectAll() {
    if (this.tabValed.length == this.tabVal.length) {
      this.tabValed = []
      this.opNamelist = []
    } else {
      this.tabValed = []
      this.opNamelist = []
      //console.log(this.tabValed.length,this.tabVal.length)
      for (let each of this.tabVal) {
        this.tabValed.push(each.operationId)
      }
      for (let each of this.tabVal) {
        this.opNamelist.push(each.operationName)
      }
    }
    //console.log(this.tabValed)
    //console.log(this.opNamelist)
  }

  //保存
  submitAssociated() {
    let url = this.server.baseUrl + 'user/functions/modifyPermission';
    let data = {
      functionId: this.functionId,
      functionName: this.functionName,
      operationId: this.tabValed,
      operationName: this.opNamelist
    }
    //console.log(data)
    this.tableListService.postRequest(url, data).then((data: any) => {
      //console.log(data.result_code)
      this.hintsService.messages(data.message,data.result_code);
      if (data.result_code === '0000') {
        $("#permission").modal("hide");
        this.tabValed = [];
        this.opNamelist = [];
      }
    }).catch();

  }
}
